﻿using MicroDBHelper;
using NLog;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WTAppletProjectHTTPS.Models;
using System.Configuration;
using System.Threading.Tasks;

namespace WTAppletProjectHTTPS.Controllers
{
    [JwtAuthentication]
    public class TestStationChangeMaterialConfirmationController : ApiController
    {
        private static readonly Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        // POST api/<controller>
        public async Task<HttpResponseMessage> Post([FromBody] TestStationChangeMaterialConfirmation TSCMC)
        {
            ReturnObject RO = new ReturnObject();
            if (TSCMC == null ||
                 string.IsNullOrEmpty(TSCMC.TSCMC_OperatorName) ||
                 string.IsNullOrEmpty(TSCMC.TSCMC_LineParting) ||
                 string.IsNullOrEmpty(TSCMC.TSCMC_ProductModel) ||
                 string.IsNullOrEmpty(TSCMC.TSCMC_ModuleModel) ||
                 string.IsNullOrEmpty(TSCMC.TSCMC_ModuleCorrectnessCheck))
            {
                Logger.Warn("输入值无效");
                RO.Status = HttpStatusCode.InternalServerError;
                RO.Message = "输入值无效";
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.InternalServerError, "输入值无效");
            }
            try
            {
                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString))
                {
                    connection.Open();
                    using (var transaction = connection.BeginTransaction())
                    {
                        try
                        {
                            var query = @"INSERT INTO TestStationChangeMaterialConfirmation 
                                    (TSCMCOperatorName, TSCMCConfirmationTime, TSCMCLineParting, TSCMCProductModel, 
                                     TSCMCModuleModel, TSCMCModuleCorrectnessCheck, TSCMCNonconformanceDescription, TSCMCNonconformanceImage,TSCMCCreateCreator,TSCMCCreateTime)
                                    VALUES 
                                    (@OperatorName, @ConfirmationTime, @LineParting, @ProductModel, 
                                     @ModuleModel, @ModuleCorrectnessCheck, @NonconformanceDescription, @NonconformanceImage,@CreateCreator,@CreateTime);
                                    SELECT CAST(scope_identity() AS int);";

                            SqlParameter[] parameters = {
                            new SqlParameter("@OperatorName", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_OperatorName },
                            new SqlParameter("@ConfirmationTime", SqlDbType.DateTime) { Value = TSCMC.TSCMC_ConfirmationTime },
                            new SqlParameter("@LineParting", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_LineParting },
                            new SqlParameter("@ProductModel", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_ProductModel },
                            new SqlParameter("@ModuleModel", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_ModuleModel },
                            new SqlParameter("@ModuleCorrectnessCheck", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_ModuleCorrectnessCheck },
                            new SqlParameter("@NonconformanceDescription", SqlDbType.NVarChar, 500) { Value = (object)TSCMC.TSCMC_NonconformanceDescription ?? DBNull.Value },
                            new SqlParameter("@NonconformanceImage", SqlDbType.NVarChar, 500) { Value = (object)TSCMC.TSCMC_NonconformanceImage ?? DBNull.Value },
                             new SqlParameter("@CreateCreator", SqlDbType.NVarChar, 50) { Value = TSCMC.TSCMC_CreateCreator },
                              new SqlParameter("@CreateTime", SqlDbType.DateTime) { Value = DateTime.Now }
                        };

                            using (var command = new SqlCommand(query, connection, transaction))
                            {
                                command.Parameters.AddRange(parameters);
                                var result = await command.ExecuteScalarAsync();
                                if (result != null)
                                {
                                    transaction.Commit();
                                    TSCMC.TSCMC_ID = Convert.ToInt32(result);
                                    Logger.Info("新增成功");
                                    RO.Status = HttpStatusCode.OK;
                                    RO.Message = "OK";
                                    RO.Data = TSCMC;
                                    return Request.CreateResponse(HttpStatusCode.OK, RO);
                                }
                                else
                                {
                                    transaction.Rollback();
                                    Logger.Error("插入记录失败");
                                    RO.Status = HttpStatusCode.InternalServerError;
                                    RO.Message = "Error";
                                    RO.Data = null;
                                    return Request.CreateResponse(HttpStatusCode.InternalServerError, "插入记录失败");
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            Logger.Error("新增失败: " + ex.Message);
                            RO.Status = HttpStatusCode.InternalServerError;
                            RO.Message = "Error" + ex.Message;
                            RO.Data = null;
                            return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
              
                Logger.Error("新增失败: " + ex.Message);
                RO.Status = HttpStatusCode.InternalServerError;
                RO.Message = "Error" + ex.Message;
                RO.Data = null;
                return Request.CreateResponse(HttpStatusCode.InternalServerError, RO);
            }
        }

    }
}